package org.example.Demo;

import org.example.pojo.Account;
import sun.security.util.Password;

import java.sql.*;
import java.util.ArrayList;

public class JDBCDemo_ResultSet01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        /*
        查询account账户中的数据，把数据封装成account对象，并且存储到arraylist集合中
        * */
        //Class.forName("com.mysql.jdbc.Driver");
        //创建连接
        String url = "jdbc:mysql://127.0.0.1:3306/firstservlet";
        String username = "root";
        String password = "Abc123+++";
        Connection conn = DriverManager.getConnection(url, username, password);

        Statement statement = conn.createStatement();

        String sql = "select * from account";

        ResultSet resultSet = statement.executeQuery(sql);
        //创建一个list集合用来存储数据库中的对象
        ArrayList<Account> list = new ArrayList<>();
        while (resultSet.next()){
            Account temp = new Account();
            //查询数据
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int money = resultSet.getInt("money");
            //把account对象添加到list集合中
            temp.setId(id);
            temp.setName(name);
            temp.setMoney(money);
            list.add(temp);
        }

        for (Account account : list) {
            System.out.println(account);
        }
        //释放资源
        conn.close();
        statement.close();
        resultSet.close();

    }
}
